#include "volumetool.h"
#include "ui_volumetool.h"

#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QDebug>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false),
    volumeRatio(20)
{
    ui->setupUi(this);
    // 设置无边框、无阴影、点击外部自动关闭的弹出式窗口(会出现在屏幕最左上面)
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 窗口增加自定义的阴影效果
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(ui->volumeWidget);
    shadowEffect->setColor("#646464");
    shadowEffect->setBlurRadius(10);
    shadowEffect->setOffset(0, 0);
    ui->volumeWidget->setGraphicsEffect(shadowEffect);

    ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    ui->volumeRatio->setText("20%");
    QRect rect = ui->outLine->geometry();
    ui->outLine->setGeometry(rect.x(), 180 - 36 + 25, rect.width(), 36);
    ui->silderBtn->move(ui->silderBtn->x(), ui->outLine->y() - ui->silderBtn->height() / 2);
    connect(ui->silenceBtn, &QPushButton::clicked, this, &VolumeTool::onSilenceBtnClicked);

    ui->volumeBox->installEventFilter(this);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void)event;
    // 绘制volumeTool界面下的三角形
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(Qt::white));
    QPolygon polygon;
    QPoint a(10 + 10, 300);
    QPoint b(10 + 10 + 60, 300);
    QPoint c(10 + 10 + 30, 300 + 20);
    polygon.append(a);
    polygon.append(b);
    polygon.append(c);

    painter.drawPolygon(polygon);
}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(ui->volumeBox == watched)
    {
        // 事件发生在volumeBox控件上
        if(event->type() == QEvent::MouseButtonPress)
        {
            // 鼠标按下事件
            calcVolume();
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            // 鼠标释放事件
            emit setMusicVolume(volumeRatio);
        }
        else if(event->type() == QEvent::MouseMove)
        {
            // 鼠标移动事件
            calcVolume();
            emit setMusicVolume(volumeRatio);
        }

        return true;
    }

    return QObject::eventFilter(watched, event);
}

void VolumeTool::calcVolume()
{
    // 获取鼠标点击时的y坐标
    int height = ui->volumeBox->mapFromGlobal(QCursor().pos()).y();

    // 验证height的合法性：[25, 205]
    height = height < 25 ? 25 : height;
    height = height > 205? 205 : height;

    ui->outLine->setGeometry(ui->outLine->x(), height, ui->outLine->width(), 205 - height);
    ui->silderBtn->move(ui->silderBtn->x(), ui->outLine->y() - ui->silderBtn->height() / 2);

    // 计算音量大小
    volumeRatio = (int)(ui->outLine->height() / (float)180 * 100);
    // 更新音量百分比
    ui->volumeRatio->setText(QString::number(volumeRatio) + "%");
}

void VolumeTool::onSilenceBtnClicked()
{
    isMuted = !isMuted;
    // 静音图标切换
    if(isMuted)
    {
        // true：表示静音
        ui->silenceBtn->setIcon(QIcon(":/images/silent.png"));
    }
    else
    {
        // false：表示非静音
        ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    }

    // 媒体静音设置，发射信号让QQMusic处理
    emit setMusicMuted(isMuted);
}
